package com.bsoft.gol.editor.dao;


import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import com.bsoft.gol.editor.editor.EditorTitleOption;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.transform.Transformers;
import com.bsoft.gol.editor.utils.CommonUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by Sangs on 2020/4/3.
 */
public abstract class EditorTitleOptionDAO extends ctd.persistence.support.hibernate.HibernateSupportDelegateDAO<EditorTitleOption> {

    public EditorTitleOptionDAO() {
        this.setKeyField("optionId");
    }

//    @DAOMethod(sql = "from EditorQuestionnaireTitle where orderNum >:orderNum and questionnaireId=:questionnaireId")
//    public abstract List<EditorQuestionnaireTitle> queryEditorQuestionnaireTitleByOrderNum(@DAOParam("orderNum") Integer orderNum,@DAOParam("questionnaireId") String questionnaireId);

    @DAOMethod(sql = "delete from EditorTitleOption where titleId = :titleId")
    public abstract void removeByTitleId(@DAOParam("titleId") String titleId);

    @DAOMethod(sql = "from EditorTitleOption where optionNum >:optionNum and titleId=:titleId")
    public abstract List<EditorTitleOption> queryEditorTitleOptionByOptionNum(@DAOParam("optionNum") Integer optionNum,@DAOParam("titleId") String titleId);

    @DAOMethod(sql = "update EditorTitleOption set optionNum=:optionNum where optionId =:optionId ")
    public abstract void updateEditorTitleOptionOrder(@DAOParam("optionId") String optionId,@DAOParam("optionNum") Integer optionNum);

    @DAOMethod(sql = "update EditorTitleOption set colNum=:colNum where optionId =:optionId ")
    public abstract void updateEditorTitleOptionColNumOrder(@DAOParam("optionId") String optionId,@DAOParam("colNum") Integer colNum);

    @DAOMethod(sql = "update EditorTitleOption set rowNum=:rowNum where optionId =:optionId ")
    public abstract void updateEditorTitleOptionRowNumOrder(@DAOParam("optionId") String optionId,@DAOParam("rowNum") Integer rowNum);


    @DAOMethod(sql = "from EditorTitleOption where titleId in (:titleIds)")
    public abstract List<EditorTitleOption> queryEditorTitleOptionByTitleIds(@DAOParam("titleIds") List<String> titleIds);

    @DAOMethod(sql = "from EditorTitleOption where titleId  =:titleId")
    public abstract List<EditorTitleOption> queryEditorTitleOptionByTitleId(@DAOParam("titleId") String titleId);

    @DAOMethod(sql = "from EditorTitleOption where optionId in (:optionIds)")
    public abstract List<EditorTitleOption> queryEditorTitleOptionByOptionIds(@DAOParam("optionIds") List<String> optionIds);

    @DAOMethod(sql = "from EditorTitleOption where colNum >:colNum and titleId=:titleId")
    public abstract List<EditorTitleOption> queryEditorTitleOptionByColNum(@DAOParam("colNum") Integer colNum,@DAOParam("titleId") String titleId);

    @DAOMethod(sql = "from EditorTitleOption where rowNum >:rowNum and titleId=:titleId")
    public abstract List<EditorTitleOption> queryEditorTitleOptionByRowNum(@DAOParam("rowNum") Integer rowNum,@DAOParam("titleId") String titleId);

    public Long queryEditorOptionCount(final EditorTitleOption qo) {
        HibernateStatelessResultAction<Long> action = new AbstractHibernateStatelessResultAction<Long>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuilder sqlBuilder = new StringBuilder();
                sqlBuilder.append("select count(1) countValue from editor_title_option a where 1=1");
                sqlBuilder.append(" and  title_id  =  '"+qo.getTitleId()+"'");
                if(qo.getPid() != null && CommonUtils.isNotEmpty(qo.getPid())) {
                    sqlBuilder.append(" and a.pid='"+qo.getPid()+"'");
                }
                sqlBuilder.append("  order by  option_num desc ");
                Query query = ss.createSQLQuery(sqlBuilder.toString());
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);

                List<Map> dbList = query.list();

                if (CommonUtils.isNotEmpty(dbList)) {
                    setResult(Long.valueOf(String.valueOf(dbList.get(0).get("countValue"))));
                } else {
                    setResult(0L);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
    /**
     * 查询列表
     * @return
     */
    public  List<EditorTitleOption> queryEditorOptionByTitleId(final EditorTitleOption qo) {
        HibernateStatelessResultAction<List<EditorTitleOption>> action = new AbstractHibernateStatelessResultAction<List<EditorTitleOption>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();

                hsql.append("select a.dict_id dictId,\n" +
                        "a.write_flag writeFlag,\n" +
                        "a.write_outward_type writeOutwardType,\n" +
                        "a.pid pid,\n" +
                        "a.row_num rowNum,\n" +
                        "a.write_height writeHeight,\n" +
                        "a.write_tips writeTips,\n" +
                        "a.import_flag importFlag,\n" +
                        "a.add_box_flag addBoxFlag,\n" +
                        "a.if_independent ifIndependent,\n" +
                        "a.data_down_limit dataDownLimit,\n" +
                        "a.create_id createId,\n" +
                        "a.col_num colNum,\n" +
                        "a.option_num optionNum,\n" +
                        "a.option_id optionId,\n" +
                        "a.create_time createTime,\n" +
                        "a.option_type optionType,\n" +
                        "a.title_id titleId,\n" +
                        "a.option_his_id optionHisId,\n" +
                        "a.write_width writeWidth,\n" +
                        "a.option_notes optionNotes,\n" +
                        "a.option_abnormal optionAbnormal,\n" +
                        "a.option_grade optionGrade,\n" +
                        "a.data_up_limit dataUpLimit,\n" +
                        "a.option_his_name optionHisName,\n" +
                        "a.option_explain optionExplain,\n" +
                        "a.option_content optionContent\n" +
                        " from editor_title_option a where 1=1");
                if(qo.getTitleId() != null && CommonUtils.isNotEmpty(qo.getTitleId())) {
                    hsql.append(" and a.title_id ='"+qo.getTitleId()+"'");
                }
                if(qo.getPid() != null && CommonUtils.isNotEmpty(qo.getPid())) {
                    hsql.append(" and a.pid='"+qo.getPid()+"'");
                }


                SQLQuery sqlQuery = ss.createSQLQuery(hsql.toString());
                sqlQuery = (SQLQuery) sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

                List<EditorTitleOption> list = sqlQuery.list();
                setResult(list);

            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

}